package com.prokarma.pkmst.controller;

import com.prokarma.pkmst.model.DeliverRequest;
import com.prokarma.pkmst.model.ModelApiResponse;
import com.prokarma.pkmst.model.SmsReportRequest;
import com.prokarma.pkmst.model.SmsSubmitResRequest;

import com.prokarma.pkmst.service.Impl.RsaSecurityServiceImpl;
import com.prokarma.pkmst.service.RsaSecurityService;
import io.swagger.annotations.*;

import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
/**
 * Api implemention
 * @author pkmst
 *
 */
@RestController
public class Path_ApiController implements Path_Api {
    private final ObjectMapper objectMapper;
    private RsaSecurityService rsaSecurityService = new RsaSecurityServiceImpl();
    @Autowired
    public Path_ApiController(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    public ResponseEntity<ModelApiResponse> apiCallbackDeliverPost(@ApiParam(value = "" ,required=true )   @RequestBody DeliverRequest request,
        @ApiParam(value = "时间戳" ,required=true) @RequestHeader(value="timestamp", required=true) String timestamp,
        @ApiParam(value = "仅仅被使用一次" ,required=true) @RequestHeader(value="nonce", required=true) String nonce,
        @ApiParam(value = "公司编号" ,required=true) @RequestHeader(value="companyNo", required=true) String companyNo,
        @ApiParam(value = "Md5(header+data)" ,required=true) @RequestHeader(value="signature", required=true) String signature,
        @RequestHeader(value = "Accept", required = false) String accept) throws Exception {

        if (rsaSecurityService.checkRSA(timestamp,nonce,companyNo,signature,objectMapper.writeValueAsString(request))) {
            // do some magic!

            if (accept != null && accept.contains("application/json")) {
                ModelApiResponse apiResponse = new ModelApiResponse();
                apiResponse.setApiStatus(ModelApiResponse.ApiStatusEnum.NUMBER_200);
                apiResponse.setCmdType(ModelApiResponse.CmdTypeEnum.DELIVERY);
                return new ResponseEntity<ModelApiResponse>(apiResponse, HttpStatus.OK);
            }
            return new ResponseEntity<ModelApiResponse>(HttpStatus.OK);
        } else {
            return new ResponseEntity<ModelApiResponse>(HttpStatus.UNAUTHORIZED);
        }




    }

    public ResponseEntity<ModelApiResponse> apiCallbackSmsreportPost(@ApiParam(value = "" ,required=true )   @RequestBody SmsReportRequest request,
        @ApiParam(value = "时间戳" ,required=true) @RequestHeader(value="timestamp", required=true) String timestamp,
        @ApiParam(value = "仅仅被使用一次" ,required=true) @RequestHeader(value="nonce", required=true) String nonce,
        @ApiParam(value = "公司编号" ,required=true) @RequestHeader(value="companyNo", required=true) String companyNo,
        @ApiParam(value = "Md5(header+data)" ,required=true) @RequestHeader(value="signature", required=true) String signature,
        @RequestHeader(value = "Accept", required = false) String accept) throws Exception {
        // do some magic!

        if (rsaSecurityService.checkRSA(timestamp,nonce,companyNo,signature,objectMapper.writeValueAsString(request))) {
            // do some magic!

            if (accept != null && accept.contains("application/json")) {

                ModelApiResponse apiResponse = new ModelApiResponse();
                apiResponse.setApiStatus(ModelApiResponse.ApiStatusEnum.NUMBER_200);
                apiResponse.setCmdType(ModelApiResponse.CmdTypeEnum.SMSREPORT);
                return new ResponseEntity<ModelApiResponse>(apiResponse, HttpStatus.OK);
            }
            return new ResponseEntity<ModelApiResponse>(HttpStatus.OK);
        } else {
            return new ResponseEntity<ModelApiResponse>(HttpStatus.UNAUTHORIZED);
        }

    }

    public ResponseEntity<ModelApiResponse> apiCallbackSubmitresPost(@ApiParam(value = "" ,required=true )   @RequestBody SmsSubmitResRequest request,
        @ApiParam(value = "时间戳" ,required=true) @RequestHeader(value="timestamp", required=true) String timestamp,
        @ApiParam(value = "仅仅被使用一次" ,required=true) @RequestHeader(value="nonce", required=true) String nonce,
        @ApiParam(value = "公司编号" ,required=true) @RequestHeader(value="companyNo", required=true) String companyNo,
        @ApiParam(value = "Md5(header+data)" ,required=true) @RequestHeader(value="signature", required=true) String signature,
        @RequestHeader(value = "Accept", required = false) String accept) throws Exception {
        if (rsaSecurityService.checkRSA(timestamp,nonce,companyNo,signature,objectMapper.writeValueAsString(request))) {
            // do some magic!

            if (accept != null && accept.contains("application/json")) {

                ModelApiResponse apiResponse = new ModelApiResponse();
                apiResponse.setApiStatus(ModelApiResponse.ApiStatusEnum.NUMBER_200);
                apiResponse.setCmdType(ModelApiResponse.CmdTypeEnum.SENDSMS);
                return new ResponseEntity<ModelApiResponse>(apiResponse, HttpStatus.OK);
            }

            return new ResponseEntity<ModelApiResponse>(HttpStatus.OK);
        } else {
            return new ResponseEntity<ModelApiResponse>(HttpStatus.UNAUTHORIZED);
        }
    }

}
